const TOKEN_KEY = 'access_token'
const REFRESH_KEY = 'refresh_token'

export default {
  getAccessToken() {
    return localStorage.getItem(TOKEN_KEY)
  },

  getRefreshToken() {
    return localStorage.getItem(REFRESH_KEY)
  },

  setTokens(access, refresh) {
    localStorage.setItem(TOKEN_KEY, access)
    localStorage.setItem(REFRESH_KEY, refresh)
  },
  setAccessTokens(access) {
    localStorage.setItem(TOKEN_KEY, access)
  },
  setaRefreshToken(refresh) {
    localStorage.setItem(REFRESH_KEY, refresh)
  },
  clearTokens() {
    localStorage.removeItem(TOKEN_KEY)
    localStorage.removeItem(REFRESH_KEY)
  },

  isAuthenticated() {
    return !!this.getAccessToken()
  },
  handleAuthFailure() {
    // 统一登出清理
    clearTokens()
    if (!window.location.pathname.includes('/login')) {
      window.location.replace('/login?reason=session_expired')
    }
  },
}
